import {
  getPage
} from '../../utils/config';
import api from '../../utils/api';
import {
  http,
  $toast
} from '../../utils/utils';
import wxApi from '../../utils/wxApi';
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressInfo: [],
    right: [{
      text: '删除',
    }, {
      text: '删除',
      style: 'color: #fa3e3e',
    }]
  },

  /**
   * 收货地址 -- 网路请求
   */
  getAddressInfo(callback, a = false) {
    http.post(api.default, {
      data: {
        action: 'addresslist',
        userid: this.data.userId
      }
    }, a).then((res) => {
      const data = res.data.data;
      callback && callback(data);
    });
  },

  /**
   * 删除事件
   */
  handleDelete(e) {
    const that = this;
    const data = e.currentTarget.dataset || e.target.dataset;
    wx.showModal({
      cancelText: '取消',
      confirmText: '确定',
      content: '是否删除收货地址？',
      success(res) {
        if (res.confirm) {
          const addressInfo = that.data.addressInfo;
          http.post(api.default, {
            data: {
              action: 'address-delete',
              userid: that.data.userId,
              address_id: data.addressId,
            }
          }).then((res) => {
            addressInfo.forEach((item, index) => {
              if (item.address_id == data.addressId) {
                addressInfo.splice(index, 1);
              }
            });
            $toast(`删除成功`, {
              icon: 'success'
            });
            that.setData({
              addressInfo: addressInfo,
            });
          })
        }
      },
    })
  },

  /**
   * 选择地址
   */
  selectClick(e) {
    const {
      dataset
    } = e.currentTarget || e.target;
    const pages = getCurrentPages()[getCurrentPages().length - 2];
    var orderid = this.data.orderid;
    wx.navigateBack({
      delta: 1,
      success() {
        if (pages !== null || pages !== undefined)
          pages.addressChange(dataset.addressId,orderid);
      }
    })
  },

  /**
   * 修改地址
   */
  editAddressTap(e) {
    const {
      dataset
    } = e.currentTarget || e.target;
    wxApi.navigateTo(getPage.addressInner, {
      id: dataset.addressId
    })
  },

  /**
   * 添加收货地址
   */
  addAddressTap() {
    wxApi.navigateTo(getPage.addressInner);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    //是否为选择地址
    if (options !== undefined && JSON.stringify(options) !== '{}') {
      this.setData({
        mode: options.mode,
        orderid: options.orderid
      })
    }

    //判断是否登录
    app.getLogin().then((res) => {
      this.setData({
        logged: res.code,
        userId: res.data.user.user_id,
      });
      //获取收货地址列表
      this.getAddressInfo((res) => {
        this.setData({
          addressInfo: res
        });
      });
    }).catch((res) => {
      this.setData({
        logged: res
      });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  }
})